/*   Copyright 2012 Julian Aloofi

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include "WindowManager.hpp"

Tacticus::WindowManager *Tacticus::WindowManager::instance = 0;

Tacticus::WindowManager &Tacticus::WindowManager::getInstance()
{
    if (!instance)
        instance = new WindowManager();
    return *instance;
}

void Tacticus::WindowManager::destroy()
{
    if (instance)
        delete instance;
    instance = 0;
}

bool Tacticus::WindowManager::isMainWindowCreated() const
{
    return mainWindowCreated;
}

Tacticus::TacticusWindow &Tacticus::WindowManager::getMainWindow()
{
    return mainWindow;
}

Tacticus::Common::ScreenCoordinate Tacticus::WindowManager::toScreenCoordinate(
    Tacticus::Common::Coordinate coordinate) const
{
    if (!mainWindowCreated)
    {
        throw Common::AssumptionException(
            "Need to create main window before converting coordinates!");
    }
    sf::Vector2f winSize = mainWindow.getView().getSize();
    return Common::ScreenCoordinate(
        coordinate.x * (winSize.x / 12.f),
        coordinate.y * (winSize.y / 10.f));
}

Tacticus::Common::Coordinate Tacticus::WindowManager::toCoordinate(
    Tacticus::Common::ScreenCoordinate screenCoordinate) const
{
    if (!mainWindowCreated)
    {
        throw Common::AssumptionException(
            "Need to create main window before converting coordinates!");
    }
    sf::Vector2f winSize = mainWindow.getView().getSize();
    return Common::Coordinate(
        (screenCoordinate.x * 12.f) / winSize.x,
        (screenCoordinate.y * 10.f) / winSize.y);
}

void Tacticus::WindowManager::createMainWindow(int width, int height,
    bool fullscreen)
{
    if (mainWindowCreated)
        throw Common::AssumptionException("Tried to create main window twice!");
    
    if (fullscreen)
    {
        mainWindow.create(sf::VideoMode(width, height), "Tacticus",
            sf::Style::Fullscreen);
    }
    else
    {
        mainWindow.create(sf::VideoMode(width, height), "Tacticus");
    }
    mainWindowCreated = true;
}
